Sentiment Analysis Of Customer Review Using Amazon Comprehend And Lambda (Boto3)

Sentiment Analysis Of Customer Review Using Amazon Comprehend And Lambda (Boto3)

Clock Icon2021.03.30

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Hello, I am Shiwani

I have tried sentiment analysis of customer review using Amazon Comprehend. I have created a lambda function to detect the sentiment from the customer review stored in S3 bucket. I hope this blog will be helpful for everyone.

What is Amazon Comprehend ?

Amazon comprehend is a natural language processing (NLP) service that uses machine learning technique. It can analyze the text and sentiment of the document. Comprehend uses machine learning which find the hidden insights and relationships in unstructured data. For more information on Amazon comprehend please check reference [4].

Features: 

  • Keyphrase Extraction,
  • Sentiment Analysis,
  • Syntax analysis
  • Entity Recognition,
  • Topic Modeling
  • Language Detection (Multiple languages ​​including Japanese)
  • Custom Entities and classification

Sentiment Analysis:

Sentiment Analysis is the process of using natural language processing to understand the sentiment (emotions) in the textual content. It is extremely useful in the social media monitoring. Amazon comprehend performs sentimental analysis on the text using any of the primary languages ​​supported by Amazon Comprehend. It also provides entities, key phrases, language and syntax of the document. It determines the sentiment of the text and classifies it into 4 classes (positive, negative, neutral, or mixed).

Sentimental analysis can be perform using any of the following operations [Reference 5]:

It is recommended to check the references [1,2,3] for the real time sentiment analysis of the text.

Sentiment analysis of the customer review stored in S3.

Services used for the sentimental analysis of customer review are S3, Lambda, IAM and Comprehend. The text document (customer review) is stored in the S3 bucket. Lambda will read the text present in that document and call the comprehend API to perform sentiment analysis ..

Please perform the following steps:

1. Add the file in the S3 bucket.

An example product review of amazon customer is used sentiment analysis which is then stored in .txt file and this file is uploaded in S3 bucket.

You can get the sample data from the link below. It is recommended to take only one customer review for this task.

https://docs.aws.amazon.com/comprehend/latest/dg/samples/tutorial-reviews-data.zip 

2. Create an IAM Role.

Please go to the IAM management console and click on lambda. Next, Attach following permissions policies:

  1. ComprehendFullAccess.
  2. AWSLambdaExecute.

Provide the Role Name and Role Description as of your choice and Create Role.

Here, you can see I have created role named as "comprehend_lambda_role" and attached the permission policies.

3. Create Lambda Function.

To create Lambda Function, please go to the lambda management console and name it as "sentimental-lambda" (you can give any name). Select runtime as python 3.8 and use the existing role which you have created to be used with lambda function.

Here, You can see the lambda function which has been created.

 

Now, Let's write the function code.

In this task, Boto3 is being used to call the Comprehend API. The following Python program detects the sentiment from the text document stored in S3. For more information on different methods to detect sentiments please check reference [6].

Please write the following python code. Deploy the code and test it.

You can see the execution results. It shows that the sentiment of customer review (document) is POSITIVE along with the Sentiment Score and Meta data. You can also store the results in another S3 bucket.

 

{'Sentiment':'POSITIVE','SentimentScore': {'Positive': 0.7621178030967712,'Negative': 0.06630583852529526,'Neutral': 0.14734360575675964,'Mixed': 0.02423272468149662},'ResponseMetadata': {'RequestId':' c7b942 -137a-47e8-af78-f4e9b29f8595','HTTPStatusCode': 200,'HTTPHeaders': {'x-amzn-requestid':'c7b94253-137a-47e8-af78-f4e9b29f8595','content-type':' application / x-amz-json-1.1','content-length': '162','date':'Sat, 27 Mar 2021 16:15:38 GMT'},'RetryAttempts': 0}}

Conclusion :  

Algorithms used for sentiment analysis are very complicated and it is very difficult to code it. I have tried sentimental analysis using NLTK before, so when I used amazon comprehend I found it very interesting and easy to use without any machine learning knowledge. This service learns from a variety of information sources including Amazon product descriptions, customer reviews, movie reviews.  It is able to provide accurate result for analyzing customer feedback, intelligent document search and automatically organizing content. 

I hope this blog is helpful for you. If you also found this service interesting then try it immediately!

References

  1. https://dev.classmethod.jp/articles/try_amazon-comprehend/
  2. https://dev.classmethod.jp/articles/add-japanese-analyzing-on-comprehend/
  3. https://dev.classmethod.jp/articles/reinvent2017-amazon-comprehend-api-explorer/
  4. https://aws.amazon.com/comprehend/
  5. https://docs.aws.amazon.com/comprehend/latest/dg/how-sentiment.html
  6. https://docs.aws.amazon.com/comprehend/latest/dg/get-started-api-sentiment.html

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.